<template>
  <div>
    <div class="title">
      登录
    </div>
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="用户名" label="用户名" placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]" />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>

</template>

<script>
import { Form, Field, Button } from 'vant'
export default {
  name: 'LoginView',
  components: {
    // 将 vant 组件注册到当前组件中
    'van-form': Form,
    'van-field': Field,
    'van-button': Button
  },
  data: function () {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    // handleLogin() {
    //   // 通过vue实例中的$router对象的push方法来进行编程式导航跳转
    //   this.$router.push('/goodsCar')
    // },
    async onSubmit(values){
      let userRegex = /^admin$/
      let psdRegex = /^123456$/
      if (!userRegex.test(this.username)) {
        alert('账号输入格式不正确！');
        return;
      }
      if (!psdRegex.test(this.password)) {
        alert('密码输入格式不正确！');
        return;
      }
      console.log('submit', values);
      const res = await this.$http.post('/user/login',{
        username:this.username,
        password:this.password
      })
      if(res.code === 200){
        this.$router.push('/goodsCar')
      }
    },
  }
}
</script>

<style>
.title {
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  text-align: center;
  width: 100%;
  color: white;
  background-color: #1989FA;
}
</style>